bt.module({builder: function(bt) {
    var Photo = {
        initSelect : function(){
            var photos = $('#photo-panel .photo-item');
            photos.click(function(e){
                var _self = $(this);
                _self.toggleClass('checked');
                _self.find('.deleted-icon').hide();
                //shift多选
                if(e.shiftKey) {
                    _self.prevUntil('.checked').filter('.unselected').each(function(i, el) {
                        $(el).toggleClass('checked');
                    });
                }
                if($("#photo-panel .checked").length > 0){
                    Hint.startLoopAnimation();
                }else{
                    Hint.stopLoopAnimation();
                }
            });
        },
        initHover : function(){
            var previewPopup = $('#preview-popup');
            $('#photo-panel .photo-item .photo').on('mouseenter',function(e){
                var _this = $(this),
                    parent = _this.parent();
                if(parent.hasClass('checked')){
                    return;
                }
                var _onTheLeft = $('#photo-panel .photo-item.unselected').index(parent) % 7 < 4;
                var _img = _this.attr('data-photo');
                previewPopup.css({
                    'left': (_this.offset().left + (_onTheLeft? 162: -238)),
                    'top': _this.offset().top
                })
                .stop(true, true).delay(800).fadeIn(200, function() {
                    previewPopup.removeClass('hidden');
                });
                previewPopup.find('img').attr('src',_img);

                _this.find('.deleted-icon').show();
            }).on('mouseleave',function(e){
                previewPopup.stop(true, true).hide();
                $(this).find('.deleted-icon').hide();
            });
            $('#photo-panel .photo-item .deleted-icon i').on('click',function(e){
                e.stopPropagation();
                if(confirm("真的要删除这张照片么？")) {
                    var parent = $(this).parents('.photo-item');
                    $.ajax({
                        url: '/trips/'+ bt.context('tripid') +'/waypoints/'+parent.attr('data-wp-id')+'/delete/',
                        type: "DELETE",
                        success: function(msg){
                            msg = eval(msg);
                            if (msg.error){
                                alert(msg.error);
                            }else{
                                parent.fadeOut();
                            }
                        }
                    });
                }
            });
        },
        initDrag : function(){
            var _selected = [];
            $("#photo-panel .photo-item").draggable({
                appendTo: "body",
                cursorAt: {left: 40, top: 20},
                cursor: "pointer",
                helper: function() {
                    var _this = $(this);
                    if ( !_this.hasClass("checked") ) {
                        _this.addClass("checked");
                    }
                    _selected = $("#photo-panel .checked");
                    var _container = $("<ul/>").addClass("dragging pr");
                    //拖动一张照片
                    if(_selected.length == 1){
                        _container.addClass('one');
                    }else{
                        //拖动多张照片
                        if(supportsTransform){
                            _container.addClass('more rotate');
                        }else{
                            _container.addClass('more');
                        }
                    }
                    _container.append($("<i>"+ _selected.length +"</i>"));
                    var _obj = _selected.clone();
                    _container.append(_obj).find(".selected-icon").remove();
                    _container.find(".photo-item").not(":eq(0)").hide();
                    return _container;
                },
                revert: "invalid",
                start: function(e, ui) {
                    Hint.startLoopAnimation();
                },
                stop: function(e, ui) {
                }
            });
        },
        checkAllPhotosLinkedToPOI : function(){
            if($("#photo-panel .unselected").length == 0) {
                $(".photo-panel").addClass("empty");
            } else {
                $(".photo-panel").removeClass("empty");
            }
        },
        linkAlreadyLinkedPhotos : function() {
            $("#photo-panel .linked").each(function() {
                var draggable = $(this);
                var droppable = $("#" + draggable.data("belongs-to-poi-id"));
                Poi.addPhotoToPOI(droppable, draggable);
            });
            this.checkAllPhotosLinkedToPOI();
        }
    }
    var poiScroller = null;
    var addPoiScroller = null;
    var map,
        marker,
        currentPoiInfo = {
            id : 0,
            value : '',
            label : '',
            location : '',
            type : '',
            meta : 'poi',
            lat : '',
            lng : '',
            tel : ''
        },
        currentPoiObj = null,
        new_poi_index = 0;
    var Poi = {
        initScrollerPanel : function(){
            poiScroller = new bt.scrollPanel.ScrollPanel(".poi-panel-scroll", ".poi-list",
                {itemSize: 172, displayCount: 6}).navigator(".navi");
            poiScroller.addItem($(".poi-panel-scroll .poi-list .poi-item").length );
            //页面上已有的poi，进行一下数据初始化
            $(".poi-panel-scroll .poi-list .poi-item").each(function() {
                $(this).data("item", {
                    meta: $(this).data("item-meta"),
                    type: $(this).data("item-type"),
                    lat: $(this).data("lat"),
                    lng: $(this).data("lng")
                });
            });
        },
        initDrop : function(obj){
            var _self = this;
            obj.droppable({
                accept: ".photo-item",
                hoverClass: "poi-item-drop-hover",
                drop: function(e, ui) {
                    var droppable = $(this);
                    var imgUrl;
                    var draggable;
                    var _container = ui.helper.children("li");
                    $.each(_container, function(index, val) {
                        var draggable = $("#"+$(val).attr("id"));
                        draggable.trigger("click");
                        _self.addPhotoToPOI(droppable, draggable);
                    });
                    droppable.addClass('poi-item-hover');
                    Hint.stopLoopAnimation();
                }
            });
        },
        addPhotoToPOI : function(droppable, draggable){

            draggable.fadeOut().removeClass("unselected");
            Photo.checkAllPhotosLinkedToPOI();
            if(droppable.data("photos")) {
                droppable.data("photos").push(draggable.attr("id"));
            } else {
                droppable.data("photos", [draggable.attr("id")]);
            }
            this.updateLinkedPhotoCount(droppable);
        },
        updateLinkedPhotoCount : function(obj){
            //更新poi列表显示的张数
            var count = $(obj).data("photos").length;
            $(obj).find('.count span').html(count);
        },
        initPoiitem : function(){
            var _self = this;

            $('#poi-panel').delegate('.poi-item','mouseenter',function(e){
                var poiItem = $(this);
                if(!poiItem.hasClass('poi-item-hover')){
                    poiItem.addClass('poi-item-hover');
                }
            }).delegate('.poi-item','mouseleave',function(e){
                var poiItem = $(this);
                if(poiItem.hasClass('poi-item-hover')){
                    poiItem.removeClass('poi-item-hover');
                }
            }).delegate('.poi-item','click',function(e){
                var poiItem = $(this);
                _self.popupPoiItemView(poiItem);
            }).delegate('.poi-item .remove','click',function(e){
                var poiItem = $(this).parents(".poi-item");
                if(confirm("真的要删除这个拍照地点么？")) {
                    var photos = poiItem.data("photos");
                    for(var i in photos) {
                        $("#" + photos[i]).fadeIn().addClass("unselected").removeClass('linked').attr("data-dismissed", 1);
                    }
                    Photo.checkAllPhotosLinkedToPOI();
                    poiItem.fadeOut(function() {
                        $(this).remove();
                        poiScroller.removeItem();
                        _self.checkAllPOIDelete();
                    });
                }
                e.stopPropagation();
            });
        },
        checkAllPOIDelete : function(){
            this.emptyText = this.emptyText || $('#poi-panel .empty-text');
            if( $('#poi-panel .poi-list .poi-item').length > 0 ){
                this.emptyText.hide();
            }else{
                this.emptyText.show();
            }
        },
        initPoiPopupView : function(){
            var _self = this;
            $("#poi-view-popup").delegate('.remove','click', function() {
                var photoItem = $(this).parents(".photo-item");
                photoItem.fadeOut().addClass("to-remove");
                if($("#poi-view-popup .content ul li").length
                    - $("#poi-view-popup .content .to-remove").length == 0) {
                    $("#poi-view-popup .content ul").addClass('empty');
                } else {
                    $("#poi-view-popup .content ul").removeClass('empty');
                }
            }).delegate('.done','click',function(){

                $("#poi-view-popup .photo-item.to-remove").each(function() {
                    var id = $(this).data("wp-id");
                    $(this).remove();
                    $("#" + id).fadeIn().addClass("unselected").removeClass('linked').attr('data-dismissed', 1);
                    Photo.checkAllPhotosLinkedToPOI();
                    var photos = $(".viewing").data("photos");
                    photos.splice($.inArray(id, photos), 1);
                    _self.updateLinkedPhotoCount($(".viewing"));
                });

                $("#poi-view-popup").dialog("close");
            });
        },
        popupPoiItemView : function(poi){
            var photos = poi.data("photos");
            $("#poi-view-popup .content ul").empty();
            poi.addClass("viewing");
            for(var p in photos) {
                var newItem = $("<li class='photo-item fl'>"
                    + "<a href='javascript:;' class='photo pr'><i class='remove pa'></i></a>"
                    + "</li>");
                $("#" + photos[p] + " .photo img").clone()
                    .prependTo(newItem.find(".photo"));
                newItem.data("wp-id", photos[p])
                    .appendTo("#poi-view-popup .content ul");
            }

            $("#poi-view-popup").dialog({
                title: "",
                closeText: "",
                modal: true,
                width: 440,
                close: function(e, ui) {
                    $(".viewing").removeClass("viewing");
                }
            });
            
            $("#poi-view-popup .close").click(function(e) {
                $("#poi-view-popup").dialog("close");
            });

            var dialog = $("#poi-view-popup").dialog("widget");

            dialog.find(".title .poi-icon")
                .addClass(poi.data("type"));
            dialog.find(".title .poi-name").text(poi.find(".title").text());
            var address = $.trim(poi.data("address"));
            if(address) {
                dialog.find(".title .poi-address").text('地址：'+poi.data("address"));        
            } 

            if($("#poi-view-popup .content ul li").length == 0) {
                dialog.find(".content ul").addClass('empty');
            } else {
                dialog.find(".content ul").removeClass('empty');
            }
        },
        initAddPoiPopup : function(){
           
            var addPoiPop = $('#add-poi-popup'),
                addPoiPopTitle = addPoiPop.find('h3'),
                addPoiPopClose = addPoiPop.find('.close'),
                contentList = addPoiPop.find('.content-list'),
                contentSearch = addPoiPop.find('.content-search'),
                searchInput = contentSearch.find(".search-input input"),
                addPoiBtn = contentSearch.find('.add-new-poi'),
                predictList = contentSearch.find('.poi-predict-list'),
                autocompleteResult = contentSearch.find('.autocomplete'),
                newAddPoiListWrap = contentSearch.find('.poi-added-list-wrap'),
                newAddPoiList = newAddPoiListWrap.find('.poi-added-list'),
                needToClose = false;

            function initPopupDialog(){
                $('#new-poi').click(function(){
                    bt.misc.showPopupAbsolute(addPoiPop);
                });
                addPoiPopClose.click(function(){
                    bt.misc.hidePopup(addPoiPop);
                });
            }
            //两个界面之间滑动
            function contentSwipe(direction,callback){
                if(direction == 'right'){
                    addPoiPopClose.hide();
                }
                contentList.animate({
                    left: direction == 'left' ?'0px':'-480px'
                },500,function(){
                    if(direction == 'left'){
                        addPoiPopClose.show();
                    }
                    if(callback){
                        callback();
                    }
                });
            }
            function initSearch(){
                addPoiScroller = new bt.scrollPanel.ScrollPanel(newAddPoiListWrap, newAddPoiList,
                {itemSize: 125, displayCount: 3}).navigator(newAddPoiListWrap.find(".poi-added-list-navi"));


                var autocompleteWidget;

                searchInput.keydown(function(e) {
                    var code = e.which;
                    if (code == 13) {
                        $(this).autocomplete("search", $(this).val());
                        e.preventDefault();
                    }
                });
                searchInput.on('input',function(e){
                    if($.trim($(this).val()) == ""){
                        autocompleteWidget && autocompleteWidget.hide();
                        addPoiBtn.show();
                    }
                });
                /*
                 * AUTO_COMPLETE DATA STRUCTURE
                 * {
                 *     id: <ciphered ID>,
                 *     meta: "[poi|place]"    # "type" ==> POI;
                 *                             # "place" ==> City/Province/Country/Attraction;
                 *     type: <type>,
                 *     value: "<POI/Place name>",
                 *     label: "<Displayed POI/Place name>",
                 *     location: "<Address/Geo location>",
                 *     lat: "<Latitude>",
                 *     lng: "<Longitude>",
                 * }
                 */
                searchInput.autocomplete({
                    delay: 500,
                    appendTo: autocompleteResult,
                    source: '/create_trip/poi_search_autocomplete/',
                    search : function(e, ui){
                        //关掉上一次的搜索结果
                        needToClose = true;
                        searchInput.autocomplete( "close" );
                    },
                    response: function(e, ui) {
                        contentSearch.find('.poi-result-list-wrap').removeClass('empty');
                        if(ui.content.length == 0){
                            addPoiBtn.show();
                        }
                    },
                    open: function(e, ui) {
                        autocompleteWidget = $(this).autocomplete("widget");
                        addPoiBtn.hide();
                        addPoiBtn.clone().appendTo(autocompleteWidget).show();
                    },
                    close: function(e, ui) {
                       
                    }
                }).autocomplete( "instance" )._renderItem = function(ul,item){
                    var poiItem = _renderItem(item);
                    poiItem.appendTo(ul);
                    return poiItem;
                };

                function _renderItem(item,secondClick){
                    var address = $.trim(item.location);
                    var poiItem = $("<li class='result-item " + item.meta + item.type + "'>")
                    .append('<i class="poi-icon"></i><p class="title">'+ item.label +'</p><p class="address">'+ address +'</p>')
                    .data("item", item);
                    if(secondClick){
                        //第二次搜索得到的热门poi，增加class区别
                        poiItem.addClass('second-result-item');
                    }
                    return poiItem;
                }
                var originalCloseMethod = searchInput.data("uiAutocomplete").close;
                searchInput.data("uiAutocomplete").close = function(event){
                    if(needToClose){
                        //只有在搜索结果重置时，才让其关闭
                        originalCloseMethod.apply( this, arguments );
                        needToClose = false;
                    }
                }
                predictList.delegate(".result-item","click", function() {
                    var _item = $(this);
                    var data = {
                        id : _item.attr('data-poi-id'),
                        meta : _item.attr('data-poi-meta'),
                        type : _item.attr('data-poi-type'),
                        label : _item.attr('data-poi-label'),
                        location : _item.attr('data-poi-location'),
                        lat : _item.attr('data-poi-lat'),
                        lng : _item.attr('data-poi-lng'),
                        tel : _item.attr('data-poi-tel')
                    };
                    if(newAddPoiList.find('li[poi-id='+data.id+']').length > 0){
                        alert('该地点已被添加');
                    }else{
                        var item = [
                            '<li class="'+ data.meta + data.type +'"><a href="javascript:;" class="pr">',
                            '<div class="title pa">'+ data.label +'</div>',
                            '<i class="poi-icon"></i>',
                            '<div class="mask pa"></div><i class="remove pa"></i>',
                            '</a></li>'
                        ].join('');
                        newAddPoiList.prepend($(item).data('item',data).attr("poi-id",data.id));
                        addPoiScroller.addItem();
                    }
                });
                autocompleteResult.delegate(".result-item","click", function() {
                    var data = $(this).data("item");
                    if(newAddPoiList.find('li[poi-id='+data.id+']').length > 0){
                        alert('该地点已被添加');
                    }else{
                        var item = [
                            '<li class="'+ data.meta + data.type +'"><a href="javascript:;" class="pr">',
                            '<div class="title pa">'+ data.label +'</div>',
                            '<i class="poi-icon"></i>',
                            '<div class="mask pa"></div><i class="remove pa"></i>',
                            '</a></li>'
                        ].join('');
                        newAddPoiList.prepend($(item).data('item',data).attr("poi-id",data.id));
                        addPoiScroller.addItem();
                    }
                    
                    searchInput.val("").blur();
                    //第一次搜索结果点击，需要再根据这个POI搜索推荐的热门poi
                    if(!$(this).hasClass('second-result-item')){
                        //关掉上一次的搜索结果
                        needToClose = true;
                        searchInput.autocomplete( "close" );

                        var type = {
                            place1 : 1,
                            place2 : 2,
                            place3 : 3,
                            place4 : 4
                        }
                        if(data.meta == 'place'){
                            type = type[data.meta+data.type]
                        }else{
                            type = 5;
                        }
                        /*
                            url : /create_trip/search_site_poi/?type=<TINYSITE_TYPE>&id=<ID>
                            param:
                            TINYSITE_TYPES.COUNTRY = 1
                            TINYSITE_TYPES.PROVINCE = 2
                            TINYSITE_TYPES.CITY = 3
                            TINYSITE_TYPES.ATTRACTION = 4
                            TINYSITE_TYPES.POI = 5
                        */
                        $.ajax({
                            type : 'GET',
                            url : '/create_trip/search_site_poi/?type='+type+'&id='+data.id,
                            success: function(resp) {
                                autocompleteWidget.empty();
                                var data = resp;
                                if(data.length > 0){
                                    autocompleteWidget.append('<div class="divide pr"><span class="pr">其他热门景点推荐</span><i></i></div>');
                                    $.each(data,function(index,item){
                                        autocompleteWidget.append(_renderItem(item,true));
                                    });
                                    autocompleteWidget.show();
                                }else{
                                    contentSearch.find('.poi-result-list-wrap').addClass('empty');
                                }
                                
                            },
                            error: function(resp) {
                            }
                        });
                    }
                });

                newAddPoiList.delegate('.remove','click',function(e){
                    $(this).parent().parent().fadeOut(function(){
                        $(this).remove();
                    });
                    addPoiScroller.removeItem();
                    e.stopPropagation();
                });

                newAddPoiList.delegate('li','click',function(){
                    //编辑已添加过的poi，如果是自己新建的则可以编辑
                    PoiAdd.initEdit($(this),$(this).data('is-new-poi')? true : false);
                    contentSwipe('right');
                });

                contentSearch.delegate('.add-new-poi','click',function(){
                    contentSwipe('right',function(){
                        //新增poi
                        PoiAdd.initAdd();
                    });
                });
                //添加已经选择的poi到最外面的poi列表
                contentSearch.find('.done').click(function(){
                    newAddPoiList.find('li').each(function () {
                        var newPOI = $("#poi-panel .poi-item-tpl").clone(),
                            data = $(this).data("item");
                        newPOI.removeClass("poi-item-tpl hidden")
                              .addClass(data.meta + data.type)
                              .data("type", data.meta + data.type)
                              .data("item", data)
                              .addClass($(this).data("is-new-poi")? "is-new-poi": "")
                              .prependTo("#poi-panel .poi-list");
                        newPOI.attr("id", $(this).attr("poi-id"));
                        newPOI.find(".title").html(data.label);
                        newPOI.data("address", data.location);
                        Poi.initDrop(newPOI);
                        $(this).remove();
                        poiScroller.addItem();
                        addPoiScroller.removeItem();
                        poiScroller.scrollTo(0);
                    });
                    bt.misc.hidePopup(addPoiPop);
                    Poi.checkAllPOIDelete();
                });
            }

            var contentAdd = addPoiPop.find('.content-add'),
                addSearch = contentAdd.find('.search-map input'),
                addSearchBtn = contentAdd.find('.search-map-btn'),
                addPoiName = $('#add-poi-detail-name'),
                addPoiAddress = $('#add-poi-detail-address'),
                addPoiTel = $('#add-poi-detail-tel'),
                savePoiBtn = contentAdd.find('.save-poi'),
                cancelPoiBtn = contentAdd.find('.cancel-poi'),
                selectorPoi = $('#add-poi-poi-type'),
                selectorPoiPanel = $('#add-poi-poi-type-panel');
            var PoiAdd = {
                init : function(){
                    bt.gmap.initMapScript(this.initAddPOIPopupMapEvent);
                    savePoiBtn.click(function(){
                        if($(this).hasClass('disabled')){
                            return;
                        }
                        if(!$.trim(addPoiName.val())){
                            alert("请输入地点名称");
                            return;
                        }
                        if(!currentPoiInfo.lat && !currentPoiInfo.lng && !$.trim(addPoiAddress.val())){
                            alert("请输入详细地址");
                            return;
                        }
                        if(!selectorPoi.data('type')){
                            alert("请选择地点分类");
                            return;
                        }
                        addPoiPopTitle.html('添加拍照地点');
                        //编辑已添加过的poi
                        if(currentPoiObj){
                            currentPoiInfo.label =  $.trim(addPoiName.val());
                            currentPoiInfo.location = $.trim(addPoiAddress.val());
                            currentPoiInfo.tel = $.trim(addPoiTel.val());
                            currentPoiInfo.type = selectorPoi.data('type');
                            contentSwipe('left',function(){
                                currentPoiObj.data('item',currentPoiInfo);
                                currentPoiObj.removeClass().addClass('poi'+currentPoiInfo.type);
                            });
                        }else{
                            //新增poi
                            currentPoiInfo.id = 'new' + new_poi_index++ ;
                            currentPoiInfo.label =  $.trim(addPoiName.val());
                            currentPoiInfo.location = $.trim(addPoiAddress.val());
                            currentPoiInfo.tel = $.trim(addPoiTel.val());
                            currentPoiInfo.type = selectorPoi.data('type');
                            contentSwipe('left',function(){
                                var item = [
                                    '<li class="poi'+ currentPoiInfo.type +'"><a href="javascript:;" class="pr">',
                                    '<div class="title pa">'+ currentPoiInfo.label +'</div>',
                                    '<i class="poi-icon"></i>',
                                    '<div class="mask pa"></div><i class="remove pa"></i>',
                                    '</a></li>'
                                ].join('');
                                newAddPoiList.prepend($(item).data('item',currentPoiInfo).data('is-new-poi',true).attr("poi-id",currentPoiInfo.id));
                                addPoiScroller.addItem();
                            });
                        }
                    });
                    cancelPoiBtn.click(function(){
                        addPoiPopTitle.html('添加拍照地点');
                        selectorPoiPanel.hide();
                        contentSwipe('left');
                    });
                    addSearch.keydown(function(e) {
                        var code = e.which;
                        if (code == 13) {
                            _searchAddress();
                            e.preventDefault();
                        }
                    });
                    addSearchBtn.click(function(){
                        if($(this).hasClass('disabled')){
                            return;
                        }
                        _searchAddress();
                    });
                    function _searchAddress(){
                        addSearch.blur();
                        var address = addSearch.val();
                        if($.trim(address)) {
                            bt.gmap.searchAddress(map, address, function(result) {
                                if(!map.bteditable) {
                                    return;
                                }
                                var latLng = result[0].geometry.location;
                                PoiAdd._addMarker(latLng.lat(), latLng.lng(), map, function(){
                                    addPoiAddress.val(currentPoiInfo.location);
                                });
                            });
                        }
                    }
                    selectorPoi.click(function(){
                        if($(this).hasClass('disabled')){
                            return;
                        }
                        selectorPoiPanel.show();
                    });
                    selectorPoiPanel.find('li').not('.text').click(function(){
                        selectorPoi.find('.select-result').html($(this).html());
                        selectorPoi.data('type',$(this).data('type'));
                        selectorPoiPanel.hide();
                    });
                },
                initAdd :function(){
                    map.bteditable = true;
                    bt.gmap.refreshMap(map);
                    if(marker) {
                        marker.setMap(null);
                        marker = null;
                    }
                    currentPoiInfo = {
                        id : 0,
                        value : '',
                        label : '',
                        location : '',
                        type : '',
                        meta : 'poi',
                        lat : '',
                        lng : '',
                        tel : ''
                    };
                    currentPoiObj = null;
                    bt.gmap.zoomToWorld(map);

                    var address = $.trim(searchInput.val());
                    searchInput.val("").blur();
                    addPoiAddress.val('').get(0).disabled = false;
                    if(address) {
                        bt.gmap.searchAddress(map, address, function(result) {
                            var latLng= result[0].geometry.location;
                            PoiAdd._addMarker(latLng.lat(), latLng.lng(), map, function(){
                                addPoiAddress.val(currentPoiInfo.location);
                            });
                        });
                    }

                    addSearch.val(address || "").get(0).disabled = false;
                    addSearchBtn.get(0).disabled = false;
                    addPoiName.val(address || "").get(0).disabled = false;
                    addPoiTel.val("").get(0).disabled = false;
                    savePoiBtn.get(0).disabled = false;
                    selectorPoi.data('type','').removeClass('disabled');
                    selectorPoi.find('.select-result').html('选择地点分类');
                    addSearchBtn.removeClass('disabled');
                    savePoiBtn.removeClass('disabled');
                    addPoiPopTitle.html('新建拍照地点');
                },
                initEdit : function(poiObj,editable){
                    map.bteditable = editable;
                    bt.gmap.refreshMap(map);
                    if(marker) {
                        marker.setMap(null);
                        marker = null;
                    }
                    currentPoiInfo = poiObj.data('item');
                    currentPoiObj = poiObj;

                    if(currentPoiInfo.lat && currentPoiInfo.lng) {
                        bt.gmap.setZoom(map, 13);
                        bt.gmap.panTo(map, currentPoiInfo.lat, currentPoiInfo.lng);
                        PoiAdd._addMarker(currentPoiInfo.lat, currentPoiInfo.lng, map);
                    } else {
                        marker = null;
                        bt.gmap.zoomToWorld(map);
                    }
                    
                    addPoiAddress.val(currentPoiInfo.location || "").get(0).disabled = !editable;
                    addSearch.val("").get(0).disabled = !editable;
                    addSearchBtn.get(0).disabled = !editable;
                    addPoiName.val(currentPoiInfo.label || "").get(0).disabled = !editable;
                    addPoiTel.val(currentPoiInfo.tel || "").get(0).disabled = !editable;
                    savePoiBtn.get(0).disabled = !editable;
                    if(editable){
                        selectorPoi.removeClass('disabled');
                    }else{
                        selectorPoi.addClass('disabled');
                    }
                    selectorPoi.find('.select-result').html('<i class="poi-icon '+ currentPoiInfo.meta + currentPoiInfo.type +'"></i><span>'+selectorPoiPanel.find('i.'+currentPoiInfo.meta +currentPoiInfo.type).next().text()+'</span>');

                    if(editable){
                        addSearchBtn.removeClass('disabled');
                        savePoiBtn.removeClass('disabled');
                        addPoiPopTitle.html('编辑拍照地点');
                    }else{
                        addSearchBtn.addClass('disabled');
                        savePoiBtn.addClass('disabled');
                        addPoiPopTitle.html('查看拍照地点');
                    }
                },
                initAddPOIPopupMapEvent : function(){
                    if(!google) {
                        return;
                    }
                    var myOptions = {
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    map = bt.gmap.createMap("map", myOptions);
                    bt.gmap.addListener(map, 'click', function(event) {
                        if(!map.bteditable) {
                            return;
                        }
                        PoiAdd._addMarker(event.latLng.lat(), event.latLng.lng(), map,function(){
                            addPoiAddress.val(currentPoiInfo.location);
                        });
                    });
                },
                _addMarker : function (lat, lng, _map, saveCallback) {
                    var _self = this;
                    if(marker) {
                        marker.setMap(null);
                        marker = null;
                    }
                    marker = bt.gmap.addMarker(_map, lat, lng);
                    _self._refreshPOILocation(_map, saveCallback);
                    if(_map.bteditable) {
                        bt.gmap.addListener(marker, 'dragend', function() {
                            _self._refreshPOILocation(_map, saveCallback);
                        });
                    } else {
                        bt.gmap.setOptions(marker, {"draggable": false});
                    }
                },
                _refreshPOILocation : function(_map, saveCallback) {
                    if(!_map.bteditable) {
                        return;
                    }
                    var latLng = marker.getPosition();

                    currentPoiInfo.lat = latLng.lat();
                    currentPoiInfo.lng = latLng.lng();

                    bt.gmap.convertCoordinateToGeo(latLng.lat(), latLng.lng(),
                        function(country_code, country, province, city) {
                            var text = ""
                                + jQuery.map([city, province, country],
                                           function(x) {
                                               return x? x: null;
                                           }).join(", ");
                            text = $.trim(text)? text: "位置";
                            currentPoiInfo.location = text;
                            if(saveCallback) {
                                saveCallback();
                            }
                        }
                    );
                }
            }

            initPopupDialog();
            initSearch();
            PoiAdd.init();
        }
    }

    var supports = (function() {
       var div = document.createElement('div'),
          vendors = 'Khtml Ms O Moz Webkit'.split(' '),
          len = vendors.length;

       return function(prop) {
          if ( prop in div.style ) return true;

          prop = prop.replace(/^[a-z]/, function(val) {
             return val.toUpperCase();
          });

          while(len--) {
             if ( vendors[len] + prop in div.style ) {
                // browser supports box-shadow. Do what you need.
                // Or use a bang (!) to test if the browser doesn't.
                return true;
             }
          }
          return false;
       };
    })();
    var supportsTransform = supports('transform');

    var Hint = {
        init: function(){
            this.hintWrapper = $('.hint-wrapper');
            this.loopAnimate = false;
        },
        timesAnimation : function(times){
            var time = 0;
            var animation = function(){
                if(time == times){
                    return;
                }else{
                    Hint.hintWrapper.css('height','0px');
                    var time2 = 0;
                    var a = setInterval(function(){
                        if(time2 == 5){
                            clearInterval(a);
                            setTimeout(function(){
                                animation();
                            },800);
                            return;
                        }
                        Hint.hintWrapper.css('height',Hint.hintWrapper.height() + 10);
                        time2++;
                    },150);
                    time++;
                }
            }
            animation();
        },
        startLoopAnimation : function(){
            if(Hint.loopAnimate){
                return;
            }
            Hint.loopAnimate = true;
            var animation = function(){
                if(!Hint.loopAnimate){
                    return;
                }else{
                    Hint.hintWrapper.css('height','0px');
                    var time2 = 0;
                    var a = setInterval(function(){
                        if(time2 == 5){
                            clearInterval(a);
                            setTimeout(function(){
                                animation();
                            },800);
                            return;
                        }
                        Hint.hintWrapper.css('height',Hint.hintWrapper.height() + 10);
                        time2++;
                    },150);
                }
            }
            animation();
        },
        stopLoopAnimation: function(){
            Hint.loopAnimate = false;
        }
    }

    var Page = {
        initPhotoPanel : function(){
            //选择图片初始化
            Photo.initSelect();
            //图片hover初始化
            Photo.initHover();
            //图片拖拽初始化
            Photo.initDrag();
        },
        initHint : function(){
            Hint.init();
            
        },
        initPoiPanel : function(){
            //poi列表滚动初始化
            Poi.initScrollerPanel();
            //图片拖拽到poi列表上drop
            Poi.initDrop($('#poi-panel .poi-item'));
            //单个poi操作初始化
            Poi.initPoiitem();
            //点击poi弹出框初始化
            Poi.initPoiPopupView();
            //将已经链接过poi的图片链接一遍
            Photo.linkAlreadyLinkedPhotos();
            //添加poi弹窗初始化
            Poi.initAddPoiPopup();
            Poi.checkAllPOIDelete();
        },
        initSubmit : function(){
            var _self = this;
            $('#btn-next').click(function(e){
                e.preventDefault();
                submit(false);
            });
            $('#step3').click(function(){
                $('#btn-next').trigger('click');
            });
            $('#step1').click(function(){
                submit(true);
            });
            function submit(isStep1){
                var _this = $('#btn-next');
                _this.hide();
                _this.next().html('正在关联地点').show();
                var data = bt.misc.initPostData();
                //收集新增的poi信息
                _self.collectNewPOIs(data);
                //收集poi和图片的链接信息
                _self.collectLinkInfo(data);
                //收集被删掉得poi和其链接的图片信息
                _self.collectDismissedPOIs(data);
                $.ajax({
                    type: "POST",
                    url: location.href,
                    data: $.param(data),
                    success: function(resp) {
                        if(isStep1){
                            window.location.href = '/trips/'+bt.context('tripid')+'/upload_photos/';
                        }else{
                            window.location.href = resp.redirect_url;
                        }
                        
                    },
                    error: function(resp) {
                        _this.next().hide();
                        _this.show();
                        bt.misc.alertGeneralError();
                    }
                });
            }
        },
        collectNewPOIs : function(data){
            var newPOIs = $(".is-new-poi");
            var newPOIData = [];
            newPOIs.each(function() {
                var id = $(this).attr("id"),
                    data = $(this).data("item"),
                    name = $(this).find(".title").html(),
                    type = data.type,
                    lat = data.lat,
                    lng = data.lng,
                    address = data.location,
                    tel = data.tel;
                newPOIData.push([id, name, type, lat, lng, address, tel]);
            });
            data["new_poi_count"] = newPOIs.length;
            data["new_poi_data"] = newPOIData;
        },
        collectLinkInfo : function(data) {
            var linkInfo = [];
            $("#poi-panel .poi-item").each(function() {
                if($(this).hasClass("poi-item-tpl")) {
                    return;
                }
                var poiId = $(this).attr("id");
                var item = $(this).data("item");
                var wpIds = $(this).data("photos");
                if(wpIds && wpIds.length > 0 ) {
                    linkInfo.push([item.meta, item.type, poiId, wpIds]);
                }
            });
            data["link_poi_data"] = linkInfo;
            data["link_poi_count"] = linkInfo.length;
        },
        collectDismissedPOIs : function(data){
            if(!data["link_poi_data"]){
                return;
            }
            var dismissedInfo = [];
            // data-dismissed=1 && .unselected
            $("#photo-panel").find(".unselected").each(function() {
                var item = $(this).attr("id");
                if ( $(this).data("dismissed") === 1 ) { dismissedInfo.push([item]); }
            });
            data["dismissed_poi_data"] = dismissedInfo;
            data["dismissed_poi_count"] = dismissedInfo.length;
        }
    }

    $(function(){
        bt.misc.initInputPlaceholder();
        Page.initPhotoPanel();
        Page.initPoiPanel();
        Page.initSubmit();
        Page.initHint();
    });
    $(window).load(function(){
        Hint.timesAnimation(3);
    });
}});